package com.globalwave.report.engine;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.junit.Test;

import com.globalwave.common.ArrayPageList;
import com.globalwave.eps.entity.ElectricityProjectCriterion;
import com.globalwave.eps.service.ElectricityProjectService;
import com.globalwave.report.template.excel.ExcelTemplate;
import com.globalwave.system.entity.User;
import com.globalwave.test.BaseTestCase;

public class ExcelGeneratorTest extends BaseTestCase {

	@Test
	public void export() throws Exception {
		
		ElectricityProjectCriterion criterion = new ElectricityProjectCriterion() ;
        
		ArrayPageList<Object> pageList = 
        	((ElectricityProjectService)this.appContext.getBean("electricityProjectService")).report(criterion,1,1000,null) ;


		ExcelTemplate template = new ExcelTemplate("com/globalwave/eps/template/projects.xls") ;

		template.load() ;
		
		Map<String, Object> context = new HashMap<String, Object>() ;

		context.put("l", pageList) ;
		context.put("u", getUser1()) ;

		template.compile(context) ;
		
        template.saveTo(new FileOutputStream("d:\\temp\\result_test3.xls", false));
	}
	
	
	public void load() throws Exception  {
		ExcelTemplate t = new ExcelTemplate("com/globalwave/report/engine/Test.xls") ;
		t.load() ;
		
		Map<String, Object> context = new HashMap<String, Object>() ;
		
		context.put("u", getUser1()) ;
		List<User> l = new ArrayList<User>() ;
		context.put("l", l) ;
		l.add(getUser1()) ;
		l.add(getUser1()) ;
		l.add(getUser1()) ;

		l.add(getUser2()) ;
		l.add(getUser2()) ;
		
		l.add(getUser3()) ;

		l.add(getUser1()) ;
		l.add(getUser1()) ;
		
		t.compile(context) ;
		
		t.saveTo(new FileOutputStream("d:\\temp\\result_test.xls", true));
	}
	
	public User getUser1() {

		User u = new User() ;
		u.setName_("小陈") ;
		u.setLogin_id("SC01") ;
		u.setUser_id(100000L) ;
		u.setEmail_("20120909");
		
		return u; 
	}
	
	public User getUser2() {

		User u = new User() ;
		u.setName_("小黄") ;
		u.setLogin_id("SC02") ;
		u.setUser_id(100001L) ;
		u.setUser_id(100001L) ;
		
		return u; 
	}
	
	public User getUser3() {

		User u = new User() ;
		u.setName_("小李") ;
		u.setLogin_id("SC03") ;
		u.setUser_id(100002L) ;
		
		return u; 
	}
}
